class GeoFenceJob {
	
	def startDelay = 300000l  //for grails app to start, 5 minute
	
	def timeout =    300000l     // execute job once in 5 minute	

	def group = "MyGroup"
	
	def dataSource
	
	def emailService
	
    def execute() {
		def c = Eventdata.createCriteria()
		def eds = c {
			eq("gfp",true)
		}
		
		if(!eds){
			println "no events to process"
			return
		}
		
		eds.each{
			eventdata->			
			def device = eventdata.device
			def devicegroup = device.devicegroup			
			def email = devicegroup.notifyEmailOrPhone
			def notifyAction = devicegroup.notifyAction			
			

			
			def mailmessege = [
							to: email, // 'to' expects a List, NOT a single email address
							subject: "Reporting for device " + device.simPhoneNumber,//
							text: eventdata.timestamp.toString()+":"+eventdata.gfpType // 'text' is the email body
						]
			
			try{				
				emailService.send(mailmessege)	
				
				//FIXME in one transaction
				eventdata.gfp = false;
				eventdata.save();
				println "email sent successfully for device:"+device.id+ " and eventdata :"+eventdata.id
			}catch(Exception e){
				e.printStackTrace()
				println "email sent failed for device :"+device.id 
			}
			
			
		}
		
	}
}
